/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.graphics;

import Engine2D.engine.Engine;
import java.awt.Dimension;
import java.awt.Image;

import org.lwjgl.opengl.GL11;

/**
 *
 * @author Garys
 */
public class GLImage {
    
    
    private GLTexture texture = null;
    private final String filename;
    private final int w;
    private final int h;
    private final int u;
    private final int v;
    
    
    /** How many times we want to tile this image */
    private Dimension tilingValues = null;
    
    /** The color to use while drawing the image */
    private GLColour colour = null;

    /**
     * 
     * @param filename
     * @param w
     * @param h
     */
    public GLImage(String filename, int w, int h) {
        this.filename = filename;
        texture = new GLTexture(filename, true);
        this.w = w;
        this.h = h;
        this.u = 0;
        this.v = 0;

        calculateTilingValues();
        
        // By default we don't want any colors in the texture
        colour = GLColour.WHITE;

    }
 
    /**
     * 
     * @param mX
     * @param mY
     * @param mAlpha
     */
    public void draw(int mX, int mY, float mAlpha) {
//        GLColour mColour =
//                new GLColour(colour.getRed(), colour.getGreen(), colour.getBlue(),
//                mAlpha);
        
        colour.setAlpha(mAlpha);
        colour.set();
        GL11.glEnable(GL11.GL_TEXTURE_2D);

        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_BLEND);

        texture.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glNormal3f(0.0f, 0.0f, 1.0f);
        GL11.glTexCoord2f(0.0f, tilingValues.height);
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY()
                - mY);
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY() - mY
                - getHeight());
        GL11.glTexCoord2f(tilingValues.width, 0.0f);
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY - getHeight());
        GL11.glTexCoord2f(tilingValues.width, tilingValues.height);
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY);
        
        
        
        
        
        GL11.glEnd(); // Done Building Our Quad (Character)
        GL11.glDisable(GL11.GL_BLEND);

        GL11.glDisable(GL11.GL_TEXTURE_2D);
        colour.reset();
    }
    
    /**
     * 
     * @return
     */
    public int getWidth() {
        return w;
    }

    
    /**
     * 
     * @return
     */
    public int getHeight() {
        return h;
    }
    
    /**
     * 
     * @param colour
     */
    public void setColor(GLColour colour) {
        this.colour = colour;
    }
    
    private void calculateTilingValues() {
        tilingValues = new Dimension();

        if (texture.getWidth() == 0 && texture.getHeight() == 0) {
            tilingValues.width = 1;
            tilingValues.height = 1;
            return;
        }

        tilingValues.width = w / texture.getWidth();
        tilingValues.height = h / texture.getHeight();

        if (tilingValues.width == 0)
            tilingValues.width = 1;
        if (tilingValues.height == 0)
            tilingValues.height = 1;
    }

    
}
